package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseListResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletPacketsContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.RedPacketBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletRedPacketsActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletPacketsPresenter implements WalletPacketsContract.Presenter {

    private WalletRedPacketsActivity context;
    private final WalletPacketsContract.View view;
    private Gson gson;

    public WalletPacketsPresenter(WalletRedPacketsActivity view,
                                  WalletRedPacketsActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }


    @Override
    public void getRedPackets(int pageNum) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "goods.gcActivityDetailListForConsumerPageFind";

        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageNum + "");
        maps.put("page_size", "20");
        String jsonPage = gson.toJson(maps);

        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        map.put("member_type_key", "consumer");
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .getRedPackets(userToken, service, jsonPage, jsonParams)
                .subscribe(new ResponseObserver<BaseListResponse<RedPacketBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseListResponse<RedPacketBean> response) {
                        view.getRedPacketsBack(response.data.getList());
                    }
                });
    }

    @Override
    public void openRedPacket(RedPacketBean packetBean) {
        UserToken bean = InfoProvider.getUserToken(App.getInstance());
        String userToken = bean.getUser_token();
        String service = "goods.gcActivityOpen";

        Map<String, String> map = new HashMap<>();
        map.put("member_id", bean.getMember_id());
        map.put("member_type_key", "consumer");
        map.put("detail_id", packetBean.getDetail_id());
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .openRedPacket(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseListResponse<Object>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseListResponse<Object> response) {
                        view.openRedPacketResult(packetBean);
                    }
                });
    }

}
